var app = angular.module('myApp', []);

app.controller('selectLocCtrl', function ($scope, $compile,$http) {
    var selectedLoc = "";
    var latitude = sessionStorage.getItem("latitude");
    var longitude = sessionStorage.getItem("longitude");

    $scope.back2Publish = function(){
        alert(selectedLoc);
        sessionStorage.setItem("selectedLoc", selectedLoc);
        sessionStorage.setItem("isRecover","true");
        window.location.href = "publish.html";
    }

    // 百度地图API功能
    var map = new BMap.Map("map");

    //利用百度地图API进行地理位置坐标转换（需要引入convertor.js这一文件）
    var gpsPoint = new BMap.Point(longitude, latitude);
    var geoc = new BMap.Geocoder();
    BMap.Convertor.translate(gpsPoint, 0, function (point) {
        geoc.getLocation(point, function (rs) {
            map.centerAndZoom(point, 18);
            map.addOverlay(new BMap.Marker(point)) ;

            var addComp = rs.addressComponents;
//            alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
//            alert('您的位置：'+rs.point.lng+','+rs.point.lat);
        });
    });

    //地图点击事件：显示所选择的地点
    map.addEventListener("click", function(e){
		var pt = e.point;
		geoc.getLocation(pt, function(rs){
			var addComp = rs.addressComponents;
			//var isConfirmed = Showbo.Msg.confirm("您所选择的地理位置是：" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
            var tempLoc = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            var isConfirmed = confirm("您所选择的地理位置是：" + tempLoc);
            if(isConfirmed){
                selectedLoc = tempLoc;
                map.clearOverlays();
                map.addOverlay(new BMap.Marker(pt));
			}
		});
	});

    //输入提示
    function G(id) {
        return document.getElementById(id);
    }
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {"input" : "selectLoc"
            ,"location" : map
    });

    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
        selectedLoc = myValue;
        setPlace();
    });

    function setPlace(){
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun(){
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp));    //添加标注
        }
        var local = new BMap.LocalSearch(map, { //智能搜索
          onSearchComplete: myFun
        });
        local.search(myValue);
    }
});